package edu.neu.javaweb.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import edu.neu.javaweb.bean.Dept;
import edu.neu.javaweb.bean.Employee;
import edu.neu.javaweb.service.DeptService;
import edu.neu.javaweb.service.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/pages/emp_find")
public class EmpFindServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        List<Employee> employeeList = new ArrayList<>();
        List<Employee> list = new EmployeeService().findAll();
        employeeList = new EmployeeService().findById(Integer.parseInt(id));

        List<Dept> dlist = new DeptService().findAll();
        request.setAttribute("emp", employeeList.get(0));
        request.setAttribute("list", list);
        request.setAttribute("dlist", dlist);
        request.getRequestDispatcher("emp_edit.jsp").forward(request, response);

        Writer out = response.getWriter();
        out.write(JSON.toJSONString(employeeList.get(0)));
        out.flush();
    }
}
